#pragma once
#include <string>
#include <cstddef>
#include <odb/nullable.hxx>
#include <odb/core.hxx>
#include "chat_session_member.hxx"

namespace MicroIMS
{
    enum class ChatSessionType
    {
        SINGLE = 1,
        GROUP = 2
    };

#pragma db object table("chat_session")
    class ChatSession
    {
    public:
        ChatSession() {}
        ChatSession(const std::string &ssid, const std::string &ssname, const ChatSessionType sstype) : _chat_session_id(ssid),
                                                                                                        _chat_session_name(ssname),
                                                                                                        _chat_session_type(sstype) {}

        std::string chat_session_id() const { return _chat_session_id; }
        void chat_session_id(std::string ssid) { _chat_session_id = ssid; }

        std::string chat_session_name() const { return _chat_session_name; }
        void chat_session_name(std::string ssname) { _chat_session_name = ssname; }

        ChatSessionType chat_session_type() const { return _chat_session_type; }
        void chat_session_type(ChatSessionType sstype) { _chat_session_type = sstype; }

    private:
        friend odb::access;
#pragma db id auto
        unsigned long _id;
#pragma db type("varchar(64)") index unique
        std::string _chat_session_id;
#pragma db type("varchar(64)")
        std::string _chat_session_name;
#pragma db type("tinyint")
        ChatSessionType _chat_session_type;
    };

// 查询某个用户所在的聊天会话，并得到该会话中另一个用户（friend_id）的信息
// 这里条件必须是css::_chat_session_type==1 && csm1.user_id=uid && csm2.user_id !=csm1.user_id
#pragma db view object(ChatSession = css)                                            \
    object(ChatSessionMember = csm1 : css::_chat_session_id == csm1::_session_id)     \
        object(ChatSessionMember = csm2 : css::_chat_session_id == csm2::_session_id) \
            query((?))
    struct SingleChatSession
    {
#pragma db column(css::_chat_session_id)
        std::string chat_session_id;
#pragma db column(csm2::_user_id)
        std::string friend_id;
    };

// 通过会话表 ChatSession 和成员表 ChatSessionMember 的关联，查询某个用户所在的群聊会话，并返回该群聊的 chat_session_id 和 chat_session_name
#pragma db view object(ChatSession = css)                                      \
    object(ChatSessionMember = csm : css::_chat_session_id == csm::_session_id) \
        query((?))
    struct GroupChatSession
    {
#pragma db column(css::_chat_session_id)
        std::string chat_session_id;
#pragma db column(css::_chat_session_name)
        std::string chat_session_name;
    };
}